package com.charm.restaurant.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.charm.restaurant.common.Result;
import com.charm.restaurant.entity.Order;
import com.charm.restaurant.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 订单控制器
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 用户下单
     */
    @PostMapping("/submit")
    public Result<String> submit(@RequestBody Order order) {
        log.info("用户下单：{}", order);
        return orderService.submit(order);
    }

    /**
     * 用户查询自己的订单
     */
    @GetMapping("/userPage")
    public Result<Page<Order>> userPage(int page, int pageSize) {
        log.info("用户查询自己的订单，page={}, pageSize={}", page, pageSize);
        return orderService.userPage(page, pageSize);
    }

    /**
     * 商家查询订单
     */
    @GetMapping("/merchantPage")
    public Result<Page<Order>> merchantPage(int page, int pageSize, String number, Integer status, String beginTime, String endTime) {
        log.info("商家查询订单，page={}, pageSize={}, number={}, status={}, beginTime={}, endTime={}",
                 page, pageSize, number, status, beginTime, endTime);
        return orderService.merchantPage(page, pageSize, number, status, beginTime, endTime);
    }

    /**
     * 查询订单详情
     */
    @GetMapping("/detail/{id}")
    public Result<Order> detail(@PathVariable Long id) {
        log.info("查询订单详情：{}", id);
        return orderService.getOrderDetail(id);
    }

    /**
     * 接单
     */
    @PutMapping("/accept/{id}")
    public Result<String> accept(@PathVariable Long id) {
        log.info("接单：{}", id);
        return orderService.acceptOrder(id);
    }

    /**
     * 配送订单
     */
    @PutMapping("/delivery/{id}")
    public Result<String> delivery(@PathVariable Long id) {
        log.info("配送订单：{}", id);
        return orderService.deliveryOrder(id);
    }

    /**
     * 完成订单
     */
    @PutMapping("/complete/{id}")
    public Result<String> complete(@PathVariable Long id) {
        log.info("完成订单：{}", id);
        return orderService.completeOrder(id);
    }

    /**
     * 取消订单
     */
    @PutMapping("/cancel/{id}")
    public Result<String> cancel(@PathVariable Long id) {
        log.info("取消订单：{}", id);
        return orderService.cancelOrder(id);
    }

    /**
     * 支付订单
     */
    @PutMapping("/pay/{id}")
    public Result<String> pay(@PathVariable Long id) {
        log.info("支付订单：{}", id);
        return orderService.payOrder(id);
    }
}
